// 04jyhs/shoppingMallAdmin/publishGoods/GoodsDetail/GoodsDetail.js
var co = require('../../../../resource/js/common.js')
var app = getApp()
var _this, _data
var _text = ''
Page({
   data: {
      Contents: [],
      text_status: '',
      old_text: '', //编辑之前的文本
      edit_text_id: '', //编辑文本的ID
      edit_image_id: '', //编辑图片的ID
      image_status: 'add', //判断活动图片是编辑还是添加
      image_edit_src: '', //预览图片的地址
      old_image_src: '',
      Activity_ID: "",
      type: '' //编辑详情还是创建详情
   },

   onLoad(opt) {
      _this = this
      _data = this.data
      console.log('活动详情opt=======', opt)
      if (opt.type == 'edit') {
         _this.setData({
            type: 'edit'
         })
      } else {
         _this.setData({
            type: 'create'
         })
      }
      _data.Activity_ID = opt.Activity_ID
      _this.getProductsDetail()
      _this.cancelMask()
   },

   //获取活动详情
   getProductsDetail() {
      co.getMealApi('jyhs/Prddetails/getPrdDetail', {
         Products_ID: app.Data.goodsList.Products_ID,
      }, function (res) {
         if (res.code === 0) {
            console.log('获取活动详情res====', res)
            _this.cancelMask()
            _this.setData({
               image_status: 'add',
               Contents: res.data
            })
         } else if (res.code === 29300) {
            _this.setData({
               Contents: []
            })
         }
      })
   },

   //获取活动详情文字内容
   getAddText(e) {
      _text = e.detail.value
   },

   //添加活动详情文字内容
   addActivityText() {
      _data.text_status = 'add'
      _this.showMask()
   },

   //编辑活动详情文字内容
   editActivityText(e) {
      var i = e.currentTarget.id
      _text = _data.Contents[i].Detail_Content
      _data.old_text = _data.Contents[i].Detail_Content
      _data.edit_text_id = _data.Contents[i].Detail_ID
      _data.text_status = 'edit'
      _this.showMask()
   },

   //显示遮罩层
   showMask() {
      _this.setData({
         _text: _text,
         addMask: false
      })
   },

   //取消遮罩层
   cancelMask() {
      _text = ''
      _this.setData({
         addMask: true
      })
   },

   //结束添加
   saveDetail() {
      if (_data.text_status == 'add') {
         if (co.empty(_text)) {
            _this.cancelMask()
         } else {
            _this.createProductsDetail('text', _text)
         }
      } else {
         if (_data.old_text == _text) {
            _this.cancelMask()
         } else if (co.empty(_text)) {
            _this.delActivityDetail(_data.edit_text_id)
         } else {
            _this.updateProductsDetail(_data.edit_text_id, _text)
         }
      }

   },

   //发布活动详情
   createProductsDetail(Type, Content) {
      co.getMealApi('jyhs/Prddetails/createPrdDetail', {
         Products_ID: app.Data.goodsList.Products_ID,
         Detail_Type: Type,
         Detail_Content: Content
      }, function (res) {
         if (res.code === 0) {
            _this.getProductsDetail()
            console.log('添加活动详情res=====', res)
         }
      })
   },

   //更新活动详情
   updateProductsDetail(Detail_ID, Detail_Content) {
      co.getMealApi('jyhs/Prddetails/updatePrdDetail', {
         Detail_ID: Detail_ID,
         Detail_Content: Detail_Content
      }, function (res) {
         if (res.code === 0) {
            console.log('更新活动详情=======', res)
            _this.getProductsDetail()
         }
      })
   },

   //预览编辑活动图片
   previewEditImage(e) {
      var i = e.currentTarget.id
      _data.edit_image_id = _data.Contents[i].Detail_ID
      _data.old_image_src = _data.Contents[i].Detail_Content
      _this.setData({
         image_status: 'edit',
         image_edit_src: _data.Contents[i].Detail_Content
      })
   },

   //选择图片
   chooseImage() {
      wx.chooseImage({
         count: 1, // 最多可以选择的图片张数，默认9
         sizeType: ['compressed'],
         sourceType: ['album', 'camera'],
         success: function (res) {
            if (res.tempFilePaths.length > 0) {
               if (_data.image_status === 'edit') {
                  _this.setData({
                     image_edit_src: res.tempFilePaths[0]
                  })
               } else {
                  //上传图片
                  _this.uploadImage(res.tempFilePaths[0])
               }
            }
         },
      })
   },

   //上传图片
   uploadImage(filePath) {
      var biz_id = app.Data.shopInfo.biz_id
      if (co.empty(biz_id))
         biz_id = 0
      if (_data.image_status === 'edit') {
         if (_data.old_image_src == _data.image_edit_src) {
            _this.setData({
               image_status: 'add'
            })
            return
         }
         filePath = _data.image_edit_src
      }
      wx.uploadFile({
         url: 'https://api.jyhs.ls293.cn/api/Upload/img',
         filePath: filePath,
         name: 'image',
         formData: {
            dir: 'product/img' + app.Data.shopInfo.Biz_ID
         },
         success: function (res) {
            console.log("【返回图片信息】===", res)
            var obj = JSON.parse(res.data)
            if (obj.code == 0) {
               if (_data.image_status === 'edit') {
                  _this.updateProductsDetail(_data.edit_image_id, obj.data.wwwPath)
               } else {
                  _this.createProductsDetail('img', obj.data.wwwPath)
               }

            } else {
               co.toastAllText(obj.msg)
            }
         }
      })
   },

   //活动详情调换排序
   upSwapSort(e) {
      var i = e.currentTarget.id
      co.getMealApi('jyhs/Prddetails/swapSortDetail', {
         Detail_ID1: _data.Contents[i].Detail_ID,
         Detail_ID2: _data.Contents[i - 1].Detail_ID
      }, function (res) {
         if (res.code === 0) {
            console.log('向上调换======', res)
            _this.getProductsDetail()
         }
      })
   },

   downSwapSort(e) {
      var i = parseInt(e.currentTarget.id)
      co.getMealApi('jyhs/Prddetails/swapSortDetail', {
         Detail_ID1: _data.Contents[i].Detail_ID,
         Detail_ID2: _data.Contents[i + 1].Detail_ID
      }, function (res) {
         if (res.code === 0) {
            console.log('向下调换======', res)
            _this.getProductsDetail()
         }
      })
   },

   //确认删除活动详情
   confirmDelContents(e) {
      var i = e.currentTarget.id;
      console.log('_data.Contents[i]========', _data.Contents[i]);
      wx.showModal({
         title: '温馨提示',
         content: '确定要删除此活动详情吗',
         success(res) {
            if (res.confirm) {
               _this.delActivityDetail(_data.Contents[i].Detail_ID)
            } else if (res.cancel) {
               return
            }
         }
      })
   },

   //删除活动详情
   delActivityDetail(Detail_ID) {
      co.getMealApi('jyhs/Prddetails/delPrdDetail', {
         Detail_ID: Detail_ID
      }, function (res) {
         if (res.code === 0) {
            console.log('删除活动详情res=======', res)
            _this.getProductsDetail()
         }
      })
   },

   //返回到活动列表
   backToActivityList() {
      wx.navigateBack({
         delta: 1
      })
   }

})